import util


class AJiaoyi:
    def __init__(self,
                 code,
                 zhangting_shijian,
                 diyizhangting_list,
                 paiban_flag,
                 money,
                 rongzi,
                 bankuai):
        self.cname = 'AJiaoyi'
        self.code = code
        self.paiban_flag = paiban_flag
        self.money = money
        self.zhangting_shijian = util.time_str_int(zhangting_shijian)
        self.diyizhangting_list = diyizhangting_list
        self.rongzi = rongzi
        self.bankuai = bankuai
        self.state = 1


class Lev2MarketDataField:
    def __init__(self,
                 SecurityID,
                 PreClosePrice,
                 OpenPrice,
                 TotalVolumeTrade,
                 TotalValueTrade,
                 AvgBidPrice,
                 TotalAskVolume,
                 AvgAskPrice,
                 HighestPrice,
                 LowestPrice,
                 LastPrice,
                 BidPrice1,
                 BidVolume1,
                 DataTimeStamp
                 ):
        self.cname = 'Lev2MarketDataField'
        self.code = SecurityID
        self.zuoshou = PreClosePrice
        self.open = OpenPrice
        self.zongLiang = TotalVolumeTrade
        self.zongJine = TotalValueTrade
        self.junJia1 = AvgBidPrice
        self.saleLiang = TotalAskVolume
        self.saleJia = AvgAskPrice
        self.high = HighestPrice
        self.low = LowestPrice
        self.close = LastPrice
        self.zhangfu = util.get_zhangfu(self.close, self.zuoshou)
        self.zhangtingJia = util.get_zhangting_jia(self.code, self.zuoshou)
        self.weituoList = []
        self.chengjiaoList = []
        self.fengdanJine = 0
        self.fengdanLiang = 0
        self.totalSaleJine = TotalAskVolume * AvgAskPrice
        self.time = DataTimeStamp
        # print('ttt  %d %d' %(BidPrice1, self.zhangtingJia))
        if TotalVolumeTrade > 0:
            self.junJia = round(TotalValueTrade / TotalVolumeTrade, 2)
        else:
            self.junJia = self.close

        if BidPrice1 >= self.zhangtingJia:
            self.fengdanLiang = BidVolume1
            self.fengdanJine = BidPrice1 * BidVolume1


#wei tuo
class Lev2OrderDetailField:
    def __init__(self, code, OrderTime, Price, Volume, Side, OrderStatus):
        self.cname = 'Lev2OrderDetailField'
        self.code = code
        self.time = OrderTime
        self.price = Price
        self.vol = Volume
        self.bs = Side
        self.state = OrderStatus
        self.jine = Price * Volume


#cheng jiao
class Lev2TransactionField:
    def __init__(self, code, Price, Volume, TradeTime, BuyNo, ExecType):
        self.cname = 'Lev2TransactionField'
        self.code = code
        self.price = Price
        self.vol = Volume
        self.time = TradeTime
        self.BuyNo = BuyNo
        # shenzhen 1 chenjiao 2 chexiao
        self.ExecType = ExecType
        self.jine = Price * Volume


class OrderField:
    def __init__(self, ExchangeID, SecurityID, Direction, LimitPrice, VolumeTotalOriginal, OrderSysID, OrderStatus,
                 OrderSubmitStatus, VolumeTraded, InsertTime):
        self.cname = 'CTORATstpOrderField'
        self.ExchangeID = ExchangeID
        self.SecurityID = SecurityID
        '''
        ///买入
        const char TORA_TSTP_D_Buy = '0';
        ///卖出
        const char TORA_TSTP_D_Sell = '1';   
        '''
        self.Direction = Direction
        if Direction == '0':
            self.DirectionText = '买入'
        elif Direction == '1':
            self.DirectionText = '卖出'
        else:
            self.DirectionText = '卖出'

        self.LimitPrice = LimitPrice
        self.VolumeTotalOriginal = VolumeTotalOriginal
        self.OrderSysID = OrderSysID

        '''
        /////////////////////////////////////////////////////////////////////////
        /// TTORATstpOrderStatusType是一个报单状态类型
        /////////////////////////////////////////////////////////////////////////
        ///预埋
        const char TORA_TSTP_OST_Cached = '0';
        ///未知
        const char TORA_TSTP_OST_Unknown = '1';
        ///交易所已接收
        const char TORA_TSTP_OST_Accepted = '2';
        ///部分成交
        const char TORA_TSTP_OST_PartTraded = '3';
        ///全部成交
        const char TORA_TSTP_OST_AllTraded = '4';
        ///部成部撤
        const char TORA_TSTP_OST_PartTradeCanceled = '5';
        ///全部撤单
        const char TORA_TSTP_OST_AllCanceled = '6';
        ///交易所已拒绝
        const char TORA_TSTP_OST_Rejected = '7';
        ///发往交易核心
        const char TORA_TSTP_OST_SendTradeEngine = '#';
        typedef char TTORATstpOrderStatusType;
        '''
        self.OrderStatus = OrderStatus
        if OrderStatus == '0':
            self.OrderStatusText = '预埋'
        elif OrderStatus == '1':
            self.OrderStatusText = '未知'
        elif OrderStatus == '2':
            self.OrderStatusText = '交易所已接收'
        elif OrderStatus == '3':
            self.OrderStatusText = '部分成交'
        elif OrderStatus == '4':
            self.OrderStatusText = '全部成交'
        elif OrderStatus == '5':
            self.OrderStatusText = '部成部撤'
        elif OrderStatus == '6':
            self.OrderStatusText = '全部撤单'
        elif OrderStatus == '7':
            self.OrderStatusText = '交易所已拒绝'
        elif OrderStatus == '7':
            self.OrderStatusText = '发往交易核心'

        self.OrderSubmitStatus = OrderSubmitStatus
        self.VolumeTraded = VolumeTraded
        self.InsertTime = InsertTime
        if OrderSubmitStatus == '0':
            self.OrderSubmitStatusText = '未提交'
        elif OrderSubmitStatus == '1':
            self.OrderSubmitStatusText = '已提交'
        elif OrderSubmitStatus == '2':
            self.OrderSubmitStatusText = '撤单未提交'
        elif OrderSubmitStatus == '3':
            self.OrderSubmitStatusText = '撤单已提交'
        elif OrderSubmitStatus == '4':
            self.OrderSubmitStatusText = '撤单已被拒绝'
        elif OrderSubmitStatus == '5':
            self.OrderSubmitStatusText = '撤单已删除'

        '''
        /////////////////////////////////////////////////////////////////////////
        /// TTORATstpOrderSubmitStatusType是一个报单提交状态类型
        /////////////////////////////////////////////////////////////////////////
        ///未提交
        const char TORA_TSTP_OSS_InsertUnSubmit = '0';
        ///已提交
        const char TORA_TSTP_OSS_InsertSubmitted = '1';
        ///撤单未提交
        const char TORA_TSTP_OSS_CancelUnSubmit = '2';
        ///撤单已提交
        const char TORA_TSTP_OSS_CancelSubmitted = '3';
        ///撤单已被拒绝
        const char TORA_TSTP_OSS_CancelRejected = '4';
        ///撤单已删除
        const char TORA_TSTP_OSS_CancelDeleted = '5';
        typedef char TTORATstpOrderSubmitStatusType;
        '''


class TradingAccountField:
    def __init__(self, UsefulMoney, FetchLimit):
        self.UsefulMoney = UsefulMoney
        self.FetchLimit = FetchLimit
        '''
        
            ///可用资金
            TTORATstpMoneyType	UsefulMoney;
        
            ///可取资金
            TTORATstpMoneyType	FetchLimit; 
        '''


# // / 投资者持仓
class PositionField:
    def __init__(self, SecurityID, SecurityName, AvailablePosition, CurrentPosition, TotalPosCost):
        self.SecurityID = SecurityID
        self.SecurityName = SecurityName
        self.AvailablePosition = AvailablePosition
        self.CurrentPosition = CurrentPosition
        self.TotalPosCost = TotalPosCost

        '''

            ///证券代码
            TTORATstpSecurityIDType	SecurityID;
        
            ///证券名称
            TTORATstpSecurityNameType	SecurityName;
            
            ///股份可用
            TTORATstpVolumeType	AvailablePosition;
        
            ///股份余额
            TTORATstpVolumeType	CurrentPosition;
            
            ///持仓成本
            TTORATstpMoneyType	TotalPosCost;
        '''

    def web_text(self, flag):
        ret_txt = ""
        if flag == 1:
            return '%s %s keyong:%d shengyu:%d chengben:%d' % (self.SecurityID, self.SecurityName, self.AvailablePosition, self.CurrentPosition, self.TotalPosCost)
        else:
            return '%s %s keyong:%d shengyu:%d chengben:%d' % (
            self.SecurityID, self.SecurityName, self.AvailablePosition, self.CurrentPosition, self.TotalPosCost)

